<html>
<%@ include file="../../common/taglibs.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title><fmt:message key="format.message.system" /></title>
</head>

<body>
<script type="text/javascript">
Ext.namespace('azure', 'azure.common');
azure.common.gamePlay = function() {
	var cm_gamePlay;
	var grid_gamePlay, store_gamePlay;
	var mainpanel;
	var toolbar_res;
	var addgamePlayForm,editgamePlayForm;
	var addgamePlayWin,editgamePlayWin;
	var addBettingWin,editBettingWin;
	var grid_Betting,store_Betting,cm_Betting;
	var addBettingForm,editBettingForm;
	
	// 創建ColumnModel
	function createCM() {
		cm_gamePlay = new Ext.grid.ColumnModel([
		{header : '<fmt:message key="format.gameplay.playname"/>',dataIndex : 'name'},
		{header : '<fmt:message key="format.gameplay.playdesc"/>',dataIndex : 'playDesc'},
		{header : '<fmt:message key="format.game.operations"/>',dataIndex : 'name',renderer : function(val, p, record) {
			var pName = record.get('name');
			var linkName = '<fmt:message key="format.game.bettingcell"/>';
			var bettingTitle = pName+"-->"+linkName;
			return '<b><a href="#" onclick="javascript:showLog(0,\''+bettingTitle+'\',\''+pName+'\');"}>'+linkName+'</a></b>';
		}}
		]);

		cm_Betting = new Ext.grid.ColumnModel([
		   {header : '<fmt:message key="format.game.bettingcell"/>',dataIndex : 'name'},
		   {header : '<fmt:message key="format.game.bettingcell.rate"/>',dataIndex : 'rate'},
		   {header : '<fmt:message key="format.game.operations"/>',dataIndex : 'name',renderer : function(val, p, record) {
				var weeks="";
				// userid = record.get('managerId');
				// weeks = record.get('yearWeek');
				var linkName = '<fmt:message key="format.game.bettingcell"/>';
				var bettingTitle = weeks+"-->"+linkName;
				return '<b><a href="#" onclick="javascript:editBettingCell();"}><fmt:message key="format.message.edit"/></a>'+
				'<a href="#" onclick="javascript:delBettingCell();"}><fmt:message key="format.message.delete"/></a></b>';
			}}]);


	}

	function createStore() {
		store_gamePlay = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({
				url : 'GamePlay_findByPage.action'
			}),
			reader : new Ext.data.JsonReader({
				root : 'pager.data',
				totalProperty : 'pager.totalSize'
			}, [{
				name : 'id',
				mapping : 'id'
			}, {
			name : 'name',
			mapping : 'name'
		}, {
			name : 'playDesc',
			mapping : 'playDesc'
		}, {
			name : 'playScript',
			mapping : 'playScript'
		}, {
			name : 'scriptType',
			mapping : 'scriptType'
		}])
		});
		store_gamePlay.load({params:{start:0,limit:15,'gameno':Ext.getCmp('gameSetPlay').idvalue}});
	store_Betting = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : 'BettingCell_findByPage.action'
		}),
		reader : new Ext.data.JsonReader({
			root : 'pager.data',
			totalProperty : 'pager.totalSize'
		}, [{
			name : 'id',
			mapping : 'id'
		},{
			name : 'gamePlayid',
			mapping : 'gamePlay.id'
		},{
		name : 'name',
		mapping : 'name'
	}, {
		name : 'rate',
		mapping : 'rate'
	}])
	});

	}

	function createToolbar(){};
	function createGrid() {
		createCM();
		createStore();
		createToolbar();

		grid_gamePlay = new Ext.grid.GridPanel({
			cm : cm_gamePlay,
			store : store_gamePlay,
			tbar:[{text:'<fmt:message key="format.message.add"/>',handler:addGamePlay},
				{text:'<fmt:message key="format.message.edit"/>',handler:editGamePlay},
				{text:'<fmt:message key="format.message.delete"/>',handler:function() {
							var dict = grid_gamePlay.getSelections()[0];
							if (dict != null) {
								Ext.MessageBox.confirm('<fmt:message key="format.message.delete"/>', '<fmt:message key="format.confirm.delete.message"/>', doDelgamePlay)
							} else {
								Ext.MessageBox.alert('<fmt:message key="format.message.prompt"/>', '<fmt:message key="format.message.selectData"/>');
							}
						}
			}],
			bbar : new Ext.PagingToolbar({
				store : store_gamePlay,
				pageSize : 15,
				displayInfo : true,
				displayMsg : '<fmt:message key="format.paging.displayMsg"/>',
				emptyMsg : '<fmt:message key="format.paging.emptyMsg"/>'
			})
		});
		grid_Betting = new Ext.grid.GridPanel({
			title:'<fmt:message key="format.game.bettingcell"/>',
			cm : cm_Betting,
			store : store_Betting,
			height :220,
			tbar:[{text:'<fmt:message key="format.message.add"/>',handler:addBettingCell}],
			bbar : new Ext.PagingToolbar({
				store : store_Betting,
				pageSize : 15,
				displayInfo : true,
				displayMsg : '<fmt:message key="format.paging.displayMsg"/>',
				emptyMsg : '<fmt:message key="format.paging.emptyMsg"/>'
		})

		});
	}

	function createUI() {
		createGrid();
		createForm();

		mainpanel = new Ext.Panel({
			frame : false,
			id:'playPanel',
			renderTo :'gamePlayDiv',
			layout:'fit',
			items : [grid_gamePlay, grid_Betting]
		});
	}

	function createForm() {
		var addGamePlayType = new Ext.form.ComboBox( {
			store : new Ext.data.SimpleStore( {
				fields : [ 'value', 'name' ],
				data : [ [ '1', 'JavaScript' ], [ '2', 'Java' ], [ '3', '.Net' ]]
			}),
			fieldLabel : '<fmt:message key="format.gameplay.scriptType"/>',
			editable : false,
			hiddenName :'gamePlay.scriptType',
			displayField : 'name',
			mode : 'local',
			triggerAction : 'all',
			maxHeight : 200,
			width : 220,
			valueField : 'value',
			selectedClass : '',
			value : '',
			selectOnFocus : true,
			autoLoad : true
		});
		addgamePlayForm = new Ext.form.FormPanel({
			labelWidth : 65,
			frame : true,
			url:'GamePlay_create.action',
			labelSeparator : ':',
			labelAlign : 'left',
			defaultType : 'textfield',
			items : [new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.gameplay.playname"/>',
				width : 220,
				name : 'gamePlay.name'
			}),new Ext.form.TextArea({
				fieldLabel : '<fmt:message key="format.gameplay.playdesc"/>',
				name : 'gamePlay.playDesc',
				width : 220,
				labelWidth : 50,
				anchor : '99%'
			}),new Ext.form.TextArea({
				fieldLabel : '<fmt:message key="format.gameplay.playscript"/>',
				name : 'gamePlay.playScript',
				width : 220,
				labelWidth : 50,
				anchor : '99%'
			}),addGamePlayType]
		});

		var editGamePlayType = new Ext.form.ComboBox( {
			store : new Ext.data.SimpleStore( {
				fields : [ 'value', 'name' ],
				data : [ [ '1', 'JavaScript' ], [ '2', 'Java' ], [ '3', '.Net' ]]
			}),
			fieldLabel : '<fmt:message key="format.gameplay.scriptType"/>',
			editable : false,
			hiddenName :'gamePlay.scriptType',
			id:'scriptType',
			displayField : 'name',
			mode : 'local',
			triggerAction : 'all',
			maxHeight : 200,
			width : 220,
			valueField : 'value',
			selectedClass : '',
			value : '',
			selectOnFocus : true,
			autoLoad : true
		});
		
		editgamePlayForm = new Ext.form.FormPanel({
			labelWidth : 65,
			frame : true,
			url:'GamePlay_update.action',
			labelSeparator : ':',
			labelAlign : 'left',
			defaultType : 'textfield',
			items : [new Ext.form.Hidden({
				id:'id',
				name : 'gamePlay.id'
			}),new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.gameplay.playname"/>',
				width : 220,
				id:'name',
				name : 'gamePlay.name'
			}),new Ext.form.TextArea({
				fieldLabel : '<fmt:message key="format.gameplay.playdesc"/>',
				id:'playDesc',
				name : 'gamePlay.playDesc',
				width : 220,
				labelWidth : 50,
				anchor : '99%'
			}),new Ext.form.TextArea({
				fieldLabel : '<fmt:message key="format.gameplay.playscript"/>',
				id:'playScript',
				name : 'gamePlay.playScript',
				width : 220,
				labelWidth : 50,
				anchor : '99%'
			}),editGamePlayType]
		});

		addBettingForm = new Ext.form.FormPanel({
			labelWidth : 65,
			frame : true,
			url:'BettingCell_createCell.action',
			labelSeparator : ':',
			labelAlign : 'left',
			defaultType : 'textfield',
			items : [
			new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.game.bettingcell"/>',
				width : 220,
				name : 'bettingCell.name'
			}),
			new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.game.bettingcell.rate"/>',
				name : 'bettingCell.rate',
				width : 220,
				labelWidth : 50,
				anchor : '99%'
			})]
		});

		editBettingForm = new Ext.form.FormPanel({
			labelWidth : 65,
			frame : true,
			url:'BettingCell_updateCell.action',
			labelSeparator : ':',
			labelAlign : 'left',
			defaultType : 'textfield',
			items : [
			new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.game.bettingcell"/>',
				width : 220,
				id:'name',
				name : 'bettingCell.name'
			}),
			new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.game.bettingcell.rate"/>',
				name : 'bettingCell.rate',
				id:'rate',
				width : 220,
				labelWidth : 50,
				anchor : '99%'
			})]
		});
	
	}
	
	function addBettingCell() {
		addBettingWin = new Ext.Window({
			width : 320,
			height : 300,
			closable : false,
			layout : 'fit',
			items : addBettingForm,
			modal : true,
			tbar : [{
				text : '<fmt:message key="format.message.confirm"/>',
				handler : createBettingCell,
				formBind : true
			}, {
				text : '<fmt:message key="format.message.cancel"/>',
				handler : function() {
					addBettingWin.hide();
				}
			}]
		});
		var rec = grid_gamePlay.getSelections()[0];
		if (rec != null) {
			addBettingWin.title = '<fmt:message key="format.title.bettingcell.add"/>';
			addBettingWin.show(Ext.getBody());
		} else {
			Ext.MessageBox.alert('<fmt:message key="format.content.Warning"/>',
				'<fmt:message key="format.bettingcell.selectPlay"/>');
		}

	}

	editBettingCell=function () {
		editBettingWin = new Ext.Window({
			width : 320,
			height : 300,
			closable : false,
			layout : 'fit',
			items : editBettingForm,
			modal : true,
			tbar : [{
				text : '<fmt:message key="format.message.confirm"/>',
				handler : updateBettingCell,
				formBind : true
			}, {
				text : '<fmt:message key="format.message.cancel"/>',
				handler : function() {
					editBettingWin.hide();
				}
			}]
		});
		var rec = grid_Betting.getSelections()[0];
		if (rec != null) {
			editBettingWin.title = '<fmt:message key="format.bettingcell.edit"/>';
			
			editBettingWin.show(Ext.getBody());
			editBettingForm.getForm().loadRecord(rec);
		} else {
			Ext.MessageBox.alert('<fmt:message key="format.content.Warning"/>', 
			'<fmt:message key="format.bettingcell.selectPlay"/>');
		}
	}	

	function addGamePlay() {
		addgamePlayWin = new Ext.Window({
			width : 320,
			height : 300,
			closable : false,
			layout : 'fit',
			items : addgamePlayForm,
			modal : true,
			tbar : [{
				text : '<fmt:message key="format.message.confirm"/>',
				handler : create,
				formBind : true
			}, {
				text : '<fmt:message key="format.message.cancel"/>',
				handler : function(){
					addgamePlayWin.hide();
				}
			}]
		});
		addgamePlayWin.title = '<fmt:message key="format.gameplay.add"/>';
		addgamePlayWin.show(Ext.getBody());
	}

	function editGamePlay() {
		editgamePlayWin = new Ext.Window({
			width : 800,
			height : 520,
			closable : false,
			layout : 'fit',
			items : editgamePlayForm,
			modal : true,
			tbar : [{
				text : '<fmt:message key="format.message.confirm"/>',
				handler: function(btn) {
					if(editgamePlayForm.getForm().isValid()){
						btn.disable();
						editgamePlayForm.getForm().submit({
							method : 'POST',
							waitMsg : '<fmt:message key="format.message.saving"/>',
							successProperty : false,
							params:{'gamePlay.game.gameno':Ext.getCmp('gameSetPlay').idvalue},
							success : function() {
								Ext.Msg.alert('<fmt:message key="format.message.success"/>', 
								'<fmt:message key="format.message.update.success"/>',function() {
									btn.enable();
									store_gamePlay.reload();
								});
							},
							failure : function() {
								Ext.Msg.alert('<fmt:message key="format.message.failure"/>', 
								'<fmt:message key="format.message.update.failure"/>');
								btn.enable();
							}
						});
						}
				},
				formBind:true
			}, {
				text : '<fmt:message key="format.message.cancel"/>',
				handler : function() {
					editgamePlayWin.hide();
				}
			}]
		});
		var rec = grid_gamePlay.getSelections()[0];
		if (rec != null) {
			editgamePlayWin.title = '<fmt:message key="format.gameplay.edit"/>';
			editgamePlayWin.show(Ext.getBody());
			editgamePlayForm.getForm().loadRecord(rec);
		} else {
			Ext.MessageBox.alert('<fmt:message key="format.content.Warning"/>', 
			'<fmt:message key="format.message.selectData"/>');
		}

	}
	
	create = function(btn) {
		if(addgamePlayForm.getForm().isValid()){
			btn.disable();
			addgamePlayForm.getForm().submit({
				method : 'POST',
				waitMsg : '<fmt:message key="format.message.saving"/>',
				successProperty : false,
				params:{'gamePlay.game.gameno':Ext.getCmp('gameSetPlay').idvalue},
				success : function() {
					Ext.Msg.alert('<fmt:message key="format.message.success"/>', 
					'<fmt:message key="format.message.save.success"/>',function() {
						btn.enable();
						store_gamePlay.reload();
					});
				},
				failure : function() {
					Ext.Msg.alert('<fmt:message key="format.message.failure"/>', 
					'<fmt:message key="format.message.save.failure"/>');
					btn.enable();
				}
			});
			}
	}

	showLog = function(id,titles,gameName){
		grid_Betting.setTitle(titles);
		store_Betting.load({params:{start:0,limit:15,gameName:gameName}});
	}

	function createBettingCell(btn){
		var rec = grid_gamePlay.getSelections()[0];
		if(addBettingForm.getForm().isValid()){
			btn.disable();
			addBettingForm.getForm().submit({
				method : 'POST',
				waitMsg : '<fmt:message key="format.message.saving"/>',
				successProperty : false,
				params:{'bettingCell.gamePlay.id':rec.data.id},
				success : function() {
					Ext.Msg.alert('<fmt:message key="format.message.success"/>', 
					'<fmt:message key="format.message.save.success"/>',function() {
						btn.enable();
						store_Betting.reload();
					});
				},
				failure : function() {
					Ext.Msg.alert('<fmt:message key="format.message.failure"/>', 
					'<fmt:message key="format.message.save.failure"/>');
					btn.enable();
				}
			});
			}

	}

	function updateBettingCell(btn){
		var rec = grid_gamePlay.getSelections()[0];
		var rec1 = grid_Betting.getSelections()[0];
		if(editBettingForm.getForm().isValid()){
			btn.disable();
			editBettingForm.getForm().submit({
				method : 'POST',
				waitMsg : '<fmt:message key="format.message.saving"/>',
				successProperty : false,
				params : {
					'bettingCell.id' : rec1.data.id,
					'bettingCell.gamePlay.id':rec.data.id
				},
				success : function() {
					Ext.Msg.alert('<fmt:message key="format.message.success"/>', 
					'<fmt:message key="format.message.update.success"/>',function() {
						btn.enable();
						store_Betting.reload();
					});
				},
				failure : function() {
					Ext.Msg.alert('<fmt:message key="format.message.failure"/>', 
					'<fmt:message key="format.message.update.failure"/>');
					btn.enable();
				}
			});
			}
	}
	function doDelgamePlay(d) {
		if (d == 'yes') {
			var r = grid_gamePlay.getSelectionModel().getSelected();
			var rec = grid_gamePlay.getSelections()[0];
			Ext.MessageBox.wait('<fmt:message key="format.content.wait"/>');
			
			Ext.Ajax.request( {
				url : 'GamePlay_delete.action',
				params : {
					'gamePlay.id' : rec.data.id
				},
				success : function(response) {
					grid_gamePlay.getStore().remove(rec);
					Ext.MessageBox.hide();
				},
				failure : function() {
					Ext.Msg.show( {
						title : '<fmt:message key="format.content.errortitle"/>',
						msg : '<fmt:message key="format.content.errormsg"/>',
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.ERROR
					});
				}
			});
			
		}
	}

	delBettingCell =function (){
		var dict = grid_gamePlay.getSelections()[0];
		if (dict != null) {
			Ext.MessageBox.confirm('<fmt:message key="format.message.delete"/>', '<fmt:message key="format.content.isdelete"/>', doDelBettingCell)
		} else {
			Ext.MessageBox.alert('<fmt:message key="format.message.prompt"/>', '<fmt:message key="format.message.selectData"/>');
		}
	}

	function doDelBettingCell(d){
		if (d == 'yes') {
			var r = grid_Betting.getSelectionModel().getSelected();
			var rec = grid_Betting.getSelections()[0];
			Ext.MessageBox.wait('<fmt:message key="format.content.wait"/>');
			Ext.Ajax.request( {
				url : 'BettingCell_deleteCell.action',
				params : {
					'bettingCell.id' : rec.data.id,
					'bettingCell.gamePlay.id':rec.data.gamePlayid
				},
				success : function(response) {
					grid_Betting.getStore().remove(rec);
					Ext.MessageBox.hide();
				},
				failure : function() {
					Ext.Msg.show( {
						title : '<fmt:message key="format.content.errortitle"/>',
						msg : '<fmt:message key="format.content.errormsg"/>',
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.ERROR
					});
				}
			});

		}
	}
	return {
		init : function() {
			createUI();
		}
	};

}();
Ext.onReady(azure.common.gamePlay.init, azure.common.gamePlay, true);
</script>

<div id="gamePlayDiv"></div>
</body>
</html>
